﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using ClassLib.Classes.Common;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraRichEdit.API.Word;
using DevExpress.XtraSplashScreen;
using ISumen;
using Sumen.Business;
using Sumen.Business.Model;
using Sumen.Business.Model.Presenter.Actor;
using Sumen.Business.Model.Presenter.Sys;
using Sumen_Stock.LookAndFeel.Sumen_System;
using System.Windows.Forms;

namespace Sumen_Stock.LookAndFeel.Entities
{
    public partial class Frm_Product : DevExpress.XtraEditors.XtraForm
    {

        Frm_Product_Edit frm_product_edit;
        private long _productId = 0;
        private FromCode _fromCode = FromCode.FrmProduct;
        private ProductPresenter _productPresenter;

        #region Form

        public Frm_Product()
        {
            InitializeComponent();
            _productPresenter = new ProductPresenter();
        }

        private void Frm_Product_Load(object sender, EventArgs e)
        {
        }

        public override void Refresh()
        {
            base.Refresh();
            LoadForm();
        }

        #endregion

        #region Button event

        private void buttonsArray1_btnEventAdd_click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            if (frm_product_edit == null)
            {
                frm_product_edit = new Frm_Product_Edit();
                frm_product_edit.EnumFormStatus = EnumFormStatus.Add;
                SplashScreenManager.CloseForm();
                frm_product_edit.ShowDialog();
            }
            else
            {
                frm_product_edit.EnumFormStatus = EnumFormStatus.Add;
                SplashScreenManager.CloseForm();
                frm_product_edit.ShowDialog();
            }

        }

        private void buttonsArray1_btnEventUpdate_click(object sender, EventArgs e)
        {
            update();
        }


        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            update();
        }

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonsArray1_btnEventDelete_click(object sender, EventArgs e)
        {
            _productId = long.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colID).ToString());
            if (_productId != null && _productId != 0)
            {
                if (ClsMsgBox.XacNhanXoaThongTin())
                {
                    if (_productPresenter.Delete(_productId) > 0)
                    {
                        ClsPublic.WriteLog(_fromCode, EnumFormStatus.Add, _productId.ToString(), "");

                    }
                }
            }
            else
            {
                ClsBaoLoi.ThongTin("Vui lòng chọn sản phẩm");
            }
        }

        #endregion


        private void btnView_Click(object sender, EventArgs e)
        {
            LoadForm();
        }

        private void update()
        {
            string sProductId = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colID) + string.Empty;

            if (sProductId != null && sProductId != "")
            {
                SplashScreenManager.ShowForm(typeof(WaitForm1));
                if (frm_product_edit == null)
                {
                    _productId = long.Parse(sProductId);
                    frm_product_edit = new Frm_Product_Edit();
                    frm_product_edit.EnumFormStatus = EnumFormStatus.Modify;
                    frm_product_edit.ProductId = _productId;
                    SplashScreenManager.CloseForm();
                    frm_product_edit.ShowDialog();
                }
                else
                {
                    _productId = long.Parse(sProductId);
                    frm_product_edit.EnumFormStatus = EnumFormStatus.Modify;
                    frm_product_edit.ProductId = _productId;
                    SplashScreenManager.CloseForm();
                    frm_product_edit.ShowDialog();
                }
            }
            else
            {
                ClsBaoLoi.ThongTin("Vui lòng ch?n s?n ph?m.");
            }
        }

        private void LoadForm()
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            var list = _productPresenter.GetByKey(popupTreeDM.ChildListPrimaryKey);
            // var list = _productPresenter.GetByKey(popupTreeDM.PrimaryKey);
            gridControl1.DataSource = list;
            gridView1.BestFitColumns();
            SplashScreenManager.CloseForm();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //gridView1.ExportToXlsx("e:\\a.xlsx");
        }

        


    }
}